import React from 'react';
import DrawerEnhance from "common/drawer/DrawerEnhance";
import {App, Button, Form, Input, Space} from "antd";
import Entity from "../../../type/Entity";
import TextArea from "antd/es/input/TextArea";
import {EngineDataContrast} from "../../../type/EngineDataContrast";
import LogFactory from "../../../log/LogFactory";
import {requestPut} from "../../../util/request";
import {Part} from "../../../type/Part";
import {useAppDispatch} from "../../../redux/hook";
import {addData} from "../../../redux/reducer/engineDataReducer";

const log = LogFactory.getLog("EngineDataAddComponent") ;
const EngineDataAddComponent = () => {

    const { message } =App.useApp() ;
    const [form] = Form.useForm<EngineDataContrast>();
    const dispatch = useAppDispatch() ;

    const onFinish = async (value: EngineDataContrast) => {
        // log.debug("表单提交的数据: " , value) ;
        let res = await requestPut<number>("/calista/engine-data-contrast",value);
        log.debug("添加发动机数据结果: ",res)
        if (res.success) {
            message.success("添加成功");
            value.key = Math.random() + "";
            dispatch(addData(value));
            form.resetFields();
        }else {
            message.error("添加失败");
        }
    };

    const dataTempChange = (ele: React.ChangeEvent<HTMLTextAreaElement>) =>{
       let val = ele.target.value.trim() ;
       if (val === "" || val === undefined){
           return;
       }
        // log.debug("解析数据: ",val) ;
        let resArr = val.split("\n");
       if (resArr.length !== 2){
           message.warning("数据格式不正确");
           return;
       }
        // log.debug("拆分结果", resArr) ;
        let regResEn = resArr[1].match(new RegExp("\\([0-9a-zA-Z_]+\\)"));
        let dataZh = resArr[1].split("(")[0];
        let unit = resArr[1].match(new RegExp("\\([% 0-9a-zA-Z\\^\\/]+\\)$"))
        log.debug("数据单位解析结果:",unit);
        if (regResEn !== null){
            let dataEn = regResEn[0].replace("(","").replace(")","");
            log.debug("数据英文名称:",dataEn);
            if (dataEn=== resArr[0]){
                // log.debug("数据英文名称一致");
                form.setFieldValue("dataNameEn",dataEn);
            }else {
                form.setFieldValue("dataNameEn","解析出多个英文变量名");
                log.warn("解析到多个英文变量名:",dataEn,resArr[0]);
            }
        }
        if (dataZh !== null){
            log.debug("数据中文名称:",dataZh);
            form.setFieldValue("dataNameZh",dataZh);
        }
        if (unit !== null){
            let resUnit = unit[0].replace("(","").replace(")","");
            log.debug("数据单位:",resUnit) ;
            form.setFieldValue("remark","数据单位: " + resUnit );
        }else {
            log.info("没有解析到数据单位")
            form.setFieldValue("remark","" );
        }
        form.setFieldValue("dataType","国六数据");
    }

    return (
        <DrawerEnhance title={"添加发动机数据"} size={"default"}>

            <TextArea onChange={dataTempChange} rows={4} />

            <Form<EngineDataContrast>
                layout={"vertical"}
                // labelCol={{ span: 6 }}
                // wrapperCol={{ span: 23,offset: 1 }}
                form={form}
                // initialValues={props.initialValues}
                onFinish={onFinish}
            >
                <Form.Item wrapperCol={{offset: 8,span: 16}}>
                    <Space size={10}>
                        <Button type="primary" htmlType="submit">添加</Button>
                        {/*<Button type="default" htmlType="reset" onReset={onReset}>重置</Button>*/}
                    </Space>
                </Form.Item>
                <Form.Item<EngineDataContrast> label="发动机数据(中文)" name={"dataNameZh"} required>
                    <Input/>
                </Form.Item>
                <Form.Item<EngineDataContrast> label="发动机数据(英文)" name={"dataNameEn"} required>
                    <Input/>
                </Form.Item>
                <Form.Item<EngineDataContrast> label="数据别名" name={"dtaNameAlias"}>
                    <Input/>
                </Form.Item>
                <Form.Item<EngineDataContrast> label="数据说明" name={"dataExplain"}>
                    <Input/>
                </Form.Item>
                <Form.Item<EngineDataContrast> label="数据类型" name={"dataType"}>
                    <Input/>
                </Form.Item>
                <Form.Item<Entity> label="备注信息" name={"remark"}>
                    <TextArea rows={3} />
                </Form.Item>
            </Form>
        </DrawerEnhance>
    );
};

export default EngineDataAddComponent;